//
//  EffectView.m
//  图片故事
//
//  Created by 大帅逼 on 16/3/4.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "EffectView.h"
#import "EffectObject.h"
#import "Header.h"
#import "FilterCollectionViewCell.h"
#import "ImageProcessViewController.h"

@interface EffectView ()<UICollectionViewDataSource,UICollectionViewDelegate>{

    NSInteger   _oldEffectIndex;
    NSInteger   _newEffectIndex;
}

@property(nonatomic,strong) UICollectionView        *effectFilterCollectionView;
@property(nonatomic,strong) UIImage                 *oldImage;
@property(nonatomic,strong) UIImageView             *contextImageView;
@property(nonatomic,strong) NSArray                 *effectFilterArray;
@property(nonatomic,strong) EffectObject            *effectObject;
@property(nonatomic,strong) NSMutableArray          *effectArray;

@end

@implementation EffectView

- (instancetype)initWithFrame:(CGRect)frame oldImage:(UIImage *)image imageView:(UIImageView *)contextImageView {

    if (self = [super initWithFrame:frame]) {
        
        _oldImage = image;
        
        _contextImageView = contextImageView;
        
        _oldEffectIndex = -1;
        
        _effectArray = [[NSMutableArray alloc] init];
        
        [self initView];
    }
    
    return self;
}

- (void) initView{

    [self addSubview:self.effectFilterCollectionView];
}

- (void) reflshEffectClickItem : (NSInteger) index {
    
    if (index != -1) {
        
        FilterCollectionViewCell *cell = (FilterCollectionViewCell *)[_effectArray objectAtIndex:_newEffectIndex];
        
        [cell.nameLabel setTextColor:[UIColor yellowColor]];
    }
    
    if (_oldEffectIndex != -1) {
        
        FilterCollectionViewCell *cell = (FilterCollectionViewCell *)[_effectArray objectAtIndex:_oldEffectIndex];
        
        [cell.nameLabel setTextColor:[UIColor whiteColor]];
    }
    
    _oldEffectIndex = _newEffectIndex;
}

#pragma mark -- 数据源方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.effectFilterArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FilterCollectionViewCell *effectCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"effect_cell" forIndexPath:indexPath];
    
    [_effectArray addObject:effectCell];
    
    [effectCell.nameLabel setText:[(FilterModel *)[self.effectFilterArray objectAtIndex:indexPath.row] filterTitle]];
    
    [effectCell.filterImageView setImage:[UIImage imageNamed:[(FilterModel *)[self.effectFilterArray objectAtIndex:indexPath.row] filterImage]]];
    
    return effectCell;
}

#pragma mark -- 代理方法

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(My_Height(80), My_Height(100));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    UIImage *newImage = [self action_itemClickEventWithIndex:indexPath.row];
    
    [_contextImageView setImage:newImage];
    
    _newEffectIndex = indexPath.row;
    
    [self reflshEffectClickItem : indexPath.row];
    
    [[ImageProcessViewController shardImagePrecessViewController] unsetFilterItem];
}

#pragma mark -- 点击事件处理
- (UIImage *) action_itemClickEventWithIndex : (NSInteger) row {

    UIImage *newImage;
    
    newImage = [EffectObject effectFilterImageWithIndex:row oldImage:[[ImageProcessViewController shardImagePrecessViewController] getOldImage]];
    
    return newImage;
}

#pragma mark -- 懒加载

- (UICollectionView *)effectFilterCollectionView {

    if (!_effectFilterCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        
        [layout setMinimumInteritemSpacing:0.0];
        
        [layout setMinimumLineSpacing:5.0];

        _effectFilterCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, My_Height(110)) collectionViewLayout:layout];
        
        [_effectFilterCollectionView registerClass:[FilterCollectionViewCell class] forCellWithReuseIdentifier:@"effect_cell"];
        
        [_effectFilterCollectionView setDataSource:self];
        
        [_effectFilterCollectionView setDelegate:self];
        
        [_effectFilterCollectionView setShowsHorizontalScrollIndicator:NO];
    }
    
    return _effectFilterCollectionView;
}

- (NSArray *)effectFilterArray {

    if (!_effectFilterArray) {
        
        _effectFilterArray = [NSArray arrayWithArray:[EffectObject readEffectFilterArray]];
    
    }
    return _effectFilterArray;
}

- (EffectObject *)effectObject {

    if (!_effectObject) {
        
        _effectObject = [[EffectObject alloc] init];
    }
    
    return _effectObject;
}

@end
